package com.mini.service;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesRequest;
import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangxiaobin
 * @title: AliCDNService
 * @description: TODO
 * @date 2019/11/2916:07
 */
@Slf4j
@Service
public class AliCDNService extends BaseService{

    @Autowired
    private IAcsClient client;

    /**
     *  刷新
     * @param url
     */
    public void refreshCaches(String url) {

        asyncTaskManager.getExecutor().submit(new Runnable() {
            @Override
            public void run() {
                RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
                request.setRegionId("cn-hangzhou");
                request.setObjectPath(url);
                try {
                    RefreshObjectCachesResponse acsResponse = client.getAcsResponse(request);
                    log.info(" ali cdn refresh response = {} " , JSON.toJSONString(acsResponse));

                } catch (Exception e) {
                    log.error(" ali cdn refresh cache error ,ex = {} ", e);
                }
            }
        });

    }
//
//    /**
//     *  预热
//     * @param url
//     */
//    public void pushCaches(String url) {
//
//        asyncTaskManager.getExecutor().submit(new Runnable() {
//            @Override
//            public void run() {
//                PushObjectCacheRequest request = new PushObjectCacheRequest();
//                request.setRegionId("cn-hangzhou");
//                request.setObjectPath(url);
//                try {
//                    PushObjectCacheResponse acsResponse = client.getAcsResponse(request);
//                    log.info(" ali cdn push response = {} " , JSON.toJSONString(acsResponse));
//
//                } catch (Exception e) {
//                    log.error(" ali cdn push cache error ,ex = {} ", e);
//                }
//            }
//        });
//
//    }

}
